%% Create new schedule for Behavior RIG 1

% File Name | warning: file will be overwritten without prompting
sch = '70dB_BB_TRAIN_gap_schedule.mat';
FN = ['C:\Behavior\Gap Detection\Schedule Files\' sch];

clear schedule trialdef

%% parameter tags

% for RPvds direct access
schedule.writeparams = {'gap_duration','trial_type','NoiseHP','NoiseLP','NoiseCF','NoiseAmp~1','NoiseAmp~2','gap_delay'};
schedule.writemodule = [1 1 1 1 1 1 1 1];
schedule.readparams  = {'trial_index','response','trial_state'};
schedule.readmodule  = [1 1 1];
schedule.nreps       = 20;
schedule.randomize   = true;
schedule.schedule    = sch;

schedule.note        = 'Broad-band gap detection log space 0.4 to 50 ms';

% for OpenEx
% schedule.writeparams = {'Stim.gap_duration','Stim.fm_sweep_delay', ...
%     'Stim.NoiseLP','Stim.NoiseHP','Stim.gap_delay','Stim.trial_type'};
% schedule.readparams  = {'Stim.trial_index'};
% schedule.OpExparams  = {'Stim.zSweep .....

%% Check yo' self
if length(schedule.writeparams) ~= length(schedule.writemodule)
	error('write params/module lengths differ!');
end

if length(schedule.readparams) ~= length(schedule.readmodule)
	error('read params/module lengths differ!');
end

%% trial defs
clear trialdef

% {1} stimulus duration;  {2} trial type: 0 = normal, 1 = catch

gaps = logspace(log10(0.1),log10(50),10);

% 0 is normal trial;  1 is catch trial
trialdef{1} = {'buddy','Condition',[0 0 0.4 0.8 1.6 3.2 6.4 12.8 25.6 50]};
trialdef{2} = {'buddy','Condition',[1 1   0   0   0   0   0    0    0  0]};

CF = 0;
HP = 2000;
LP = 43000;

% noise filter
trialdef{3} = {'buddy','Noise',HP};
trialdef{4} = {'buddy','Noise',LP};
trialdef{5} = {'buddy','Noise',CF};

% noise amplitude (calibrated 70 dB SPL)
trialdef{6} = {'buddy','Noise',[0.114024979]};
trialdef{7} = {'buddy','Noise',[0.089227762]};
    
% trial duration
trialdef{8} = {'randomized','round',[3000 8000]};

%% Get Unique Trials
schedule = BHVR_AddTrial(schedule,trialdef);





%% Now Make Schedule 2
% parameter tags

% for RPvds direct access
schedule(2).writeparams = {'gap_duration','trial_type','NoiseHP','NoiseLP', ...
    'NoiseAmp~1','NoiseAmp~2','gap_delay'};
schedule(2).writemodule = [1 1 1 1 1 1 1];
schedule(2).readparams  = {'trial_index','response','trial_state'};
schedule(2).readmodule  = [1 1 1];
schedule(2).nreps       = 10;
schedule(2).randomize   = true;
schedule(2).schedule    = sch;

%% Check yo' self
if length(schedule(2).writeparams) ~= length(schedule(2).writemodule)
	error('write params/module lengths differ!');
end

if length(schedule(2).readparams) ~= length(schedule(2).readmodule)
	error('read params/module lengths differ!');
end

%% Initialize with first trial
schedule(2).trials = [50,0,2000,43000,0.114024979,0.089227762,750];


%% Adaptive trials
schedule(2).AdaptiveTrials.increment =  250; % in milliseconds
schedule(2).AdaptiveTrials.decrement = -250;
schedule(2).AdaptiveTrials.incrafter = 3; % trials
schedule(2).AdaptiveTrials.decrafter = 3;
schedule(2).AdaptiveTrials.min       = 750; % in milliseconds
schedule(2).AdaptiveTrials.max       = 15000;
schedule(2).AdaptiveTrials.parameter = 'gap_delay';






%% Save

save(FN,'schedule');

